//
//  CinemaDetailViewController.m
//  WisdomZhuHai
//
//  Created by Ryan on 14-12-12.
//  Copyright (c) 2014年 wyd. All rights reserved.
//

#import "CinemaDetailViewController.h"
#import "HotRateView.h"

static const CGFloat TITLECELL_HEIGHT = 100;
static const CGFloat ATTRIBUTECELL_HEIGHT = 50;

@interface CinemaDetailViewController ()

@property (nonatomic,strong)NSDictionary* cinema;

@end

@implementation CinemaDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tableView.backgroundColor = [Common colorWithRGB:0Xffffff];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    [self setEnableRefresh:YES];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.f)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.f)];
    self.title = @"电影院详情";
    [self getSingleCinemaDetail];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Protocol

- (void)getSingleCinemaDetail
{
    [[ProtocolManager shareInstance] getStoreDetail:self.storeId sendType:@"影院信息" target:self selector:@selector(getSingleCinemaDetailCallback:)];
}

#pragma mark - Callback
- (void)getSingleCinemaDetailCallback:(NSDictionary*)dic
{
    NSLog(@"getSingleCinemaDetailCallback %@",dic);
    self.cinema = [Common valueForKey:P_CINEMA dic:dic];
    [self.tableView reloadData];
}

#pragma mark - cell
- (void)cinemaTitleCell:(UITableViewCell*)cell
{
    NSString *name = [Common valueForKey:P_NAME dic:self.cinema];
    CGFloat star = [[Common valueForKey:P_RATE dic:self.cinema] floatValue] / 2;
    
    UILabel *nameLabel = (UILabel*)[cell viewWithTag:1001];
    nameLabel.text = name;
    
    [self setupStarViewForCell:cell star:star];
}

//设置星级
- (void)setupStarViewForCell:(UITableViewCell*)cell star:(CGFloat)star
{
    UIView *starView = [cell viewWithTag:1005];
    for (UIView* view in starView.subviews) {
        [view removeFromSuperview];
    }
    
    HotRateView *hotView = [[HotRateView alloc] initWithHotRate:star];
    CGRect frame = hotView.frame;
    frame.origin.x = 0;
    frame.origin.y = (starView.bounds.size.height - hotView.bounds.size.height)/2;
    hotView.frame = frame;
    [starView addSubview:hotView];
}

- (void)attributeCell:(UITableViewCell*)cell index:(int)index
{
    if (index == 0) {
        UIImageView *attributeImg = (UIImageView*)[cell viewWithTag:1101];
        attributeImg.image = [UIImage imageNamed:@"movie/movie_phone.png"];
        UILabel *phoneLabel = (UILabel*)[cell viewWithTag:1102];
        NSString *phone = [Common valueForKey:P_PHONE dic:self.cinema];
        phoneLabel.textColor = [UIColor blueColor];
        phoneLabel.text = phone;
    }
    else if (index == 1) {
        UIImageView *attributeImg = (UIImageView*)[cell viewWithTag:1101];
        attributeImg.image = [UIImage imageNamed:@"movie/movie_local.png"];
        UILabel *localLabel = (UILabel*)[cell viewWithTag:1102];
        NSString *address = [Common valueForKey:P_ADRESS dic:self.cinema];
        localLabel.text = address;
    }
}

- (void)specialCell:(UITableViewCell*)cell index:(int)index
{
    if (index == 0) {
        UIImageView *specialImg = (UIImageView*)[cell viewWithTag:1101];
        specialImg.image = [UIImage imageNamed:@"movie/movie_park.png"];
        UILabel *specialLabel = (UILabel*)[cell viewWithTag:1102];
        //NSString *special = [Common valueForKey:P_PHONE dic:self.cinema];
        specialLabel.text =@"免费停车";
    }
    else if (index == 1) {
        UIImageView *specialImg = (UIImageView*)[cell viewWithTag:1101];
        specialImg.image = [UIImage imageNamed:@"movie/movie_glasses.png"];
        UILabel *specialLabel = (UILabel*)[cell viewWithTag:1102];
        //NSString *special = [Common valueForKey:P_ADRESS dic:self.cinema];
        specialLabel.text = @"3D眼镜";
    }
}

#pragma mark - tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    else if (section == 1) {
        return 2;
    }
    else if (section == 2) {
        return 2;
    }
    else
        return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = nil;
    switch (indexPath.section) {
        case 0:
            cell = [tableView dequeueReusableCellWithIdentifier:@"CinemaTitleCell"];
            if (cell == nil)
            {
                cell = [[NSBundle mainBundle] loadNibNamed:@"CinemaDetailCell"  owner:self options:nil][0];
            }
            [self cinemaTitleCell:cell];
            break;
            
        case 1:
            cell = [tableView dequeueReusableCellWithIdentifier:@"AttributeCell"];
            if (cell == nil) {
                cell = [[NSBundle mainBundle] loadNibNamed:@"CinemaDetailCell"  owner:self options:nil][1];
            }
            [self attributeCell:cell index:indexPath.row];
            break;
        
        case 2:
            cell = [tableView dequeueReusableCellWithIdentifier:@"SpecialCell"];
            if (cell == nil) {
                cell = [[NSBundle mainBundle] loadNibNamed:@"CinemaDetailCell"  owner:self options:nil][1];
            }
            [self specialCell:cell index:indexPath.row];
            break;
        default:
            cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
            // Configure the cell...
            if(cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
            }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 2) {
        return 44;
    }
    return 0;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    view.backgroundColor = [Common colorWithRGB:0xeaeaea];
    if (section == 2) {
        UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 100, 20)];
        lb.backgroundColor = [UIColor clearColor];
        lb.font = [UIFont systemFontOfSize:14];
        lb.text = @"特色信息";
        [view addSubview:lb];
    }
    return view;
}

//每一个cell定义不同的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0) {
        return TITLECELL_HEIGHT;
    }
    else if (indexPath.section == 1 || indexPath.section == 2) {
        return ATTRIBUTECELL_HEIGHT;
    }
    else{
        return 100;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            NSLog(@"call");
//            NSString* phone = [NSString stringWithFormat:@"telprompt:%@",[Common valueForKey:P_PHONE dic:self.cinema]];
//            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phone]];//@"telprompt:13750002593"
            UIWebView*callWebview =[[UIWebView alloc] init];
            
            NSString *telUrl = [NSString stringWithFormat:@"tel:%@",[Common valueForKey:P_PHONE dic:self.cinema]];
            
            NSURL *telURL =[NSURL URLWithString:telUrl];// 貌似tel:// 或者 tel: 都行
            
            [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
            
            //记得添加到view上
            
            [self.view addSubview:callWebview];
        }
    }
}

@end
